package com.cqc.aiyagril2.module.home;

import com.cqc.aiyagril2.model.CategoryResult;
import com.cqc.aiyagril2.net.RetrofitHelper;
import com.cqc.aiyagril2.utils.NetErrorUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ${cqc} on 2018/1/12.
 * p
 */

public class HomePresenter implements HomeContract.IHomePresenter {

    private  CompositeDisposable disposable;
    private HomeContract.IHomeView homeView;

    public HomePresenter(HomeContract.IHomeView homeView,CompositeDisposable disposable) {
        this.homeView = homeView;
        this.disposable = disposable;
    }

//    @Override
//    public void subscrible() {
//
//    }

    @Override
    public void unSubscrible() {
        if (disposable != null && disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    @Override
    public void getHomeBanners() {
        disposable.add(RetrofitHelper.createGankApi()
                .getCategoryData("福利",5,1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CategoryResult>() {
                    @Override
                    public void accept(CategoryResult categoryResult) throws Exception {

                        homeView.getHomeBannersSuccess(categoryResult);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        String error=NetErrorUtil.handleThrowable(throwable);
                        homeView.getHomeBannersFail(error);
                    }
                })
        );
    }
}
